# -*- coding:utf-8 -*-

import binascii
import hashlib
import hmac
import random
import requests
import json
import time
import base64
import xlwt
from datetime import datetime
from json_data import case_list, users

pass    # todo ###########################################  跑数据  1000

cy = 1
table1 = xlwt.Workbook(encoding='utf-8')
style = xlwt.XFStyle()
alignment = xlwt.Alignment()
alignment.horz = 0x02
alignment.vert = 0x01
style.alignment = alignment

sheet2 = table1.add_sheet("sheet1")
sheet2.write(0, 0, '箱子', style)
sheet2.write(0, 1, '总金额', style)
sheet2.write(0, 2, '总出', style)
sheet2.write(0, 3, '比例', style)

for case in case_list:
    for user in users:
        user.update(money=user["old"], state=True, round=0, cost=0, get=0, run_number=0)

    for user in users:
        y = 1
        for number in range(1000):
            # 修改回合数
            if user['round'] == 0:
                _ini_round = random.randint(0, 100000)
                user.update(round=_ini_round)
                user['round'] = _ini_round
            user_round = user['round'] + 1
            user.update(round=user_round)
            secret = binascii.unhexlify(user['hash'])
            seed = (user['seed'] + "-" + str(user_round)).encode("utf8")
            roll_hash = hmac.new(secret, seed, hashlib.sha512).hexdigest()
            subHash = roll_hash[:7]
            sixteen_int = int(subHash, 16)
            roll = sixteen_int % 1000000 + 1

            if user["money"] >= case["price"]:
                drops = case["drop"]
                item = {}
                for key in drops:
                    roll_min_max = key.split("-")
                    if int(roll_min_max[0]) < roll <= int(roll_min_max[1]):
                        item = drops[key]
                        break
                if item == {}:
                    continue

                # 更新用户信息
                user.update(cost=round(user['cost'] + case["price"], 2))
                # 获取饰品价格大于成本价的3倍不买
                if item["price"] > case["price"] * 3:
                    user.update(get=round(user['get'] + item["price"], 2))
                    user.update(money=round(user['money'] - case["price"], 2))
                else:
                    #  需减去金额
                    reduce_money = case["price"] - item["price"]
                    user.update(money=round(user['money'] - reduce_money, 2))
                user.update(run_number=number)

                print(user['user'], case['name'], item['name'], case["price"], item["price"], case["price"] * 3)
                print(user['user'], user['old'], user['money'], user['round'], case['name'], item['name'], number, user["cost"], user["get"])
                y += 1
            else:
                print("no money", user['user'], user['money'])
                break

    money_dict = {100: 100, 200: 200, 560: 500, 1130: 1000, 2260: 2000, 3540: 3000, 5860: 5000, 12060: 10000, 23690: 20000, 59960: 50000}
    y = 1
    out_total = 0
    in_total = 0
    for u in users:
        charge = money_dict[u["old"]]
        out_total += charge
        in_total += float(u["get"])
        y += 1

    sheet2.write(cy, 0, case["name"], style)
    sheet2.write(cy, 1, out_total, style)
    sheet2.write(cy, 2, in_total, style)
    sheet2.write(cy, 3, round(in_total / out_total * 100, 2), style)
    cy += 1


table1.save("{} 单人开箱(3倍留背包)集合.xls".format(datetime.now().strftime("%Y-%m-%d %H%M%S")))